//This header contains thread struct and functions
//NOTE:I assume, what kernelspace thread is equal to user land, so i use only one common struct
#include "defines/types.h"

struct mem_map
{
    //common data registers
    reg_t eax;
    reg_t ebx;
    reg_t ecx;
    reg_t edx;
    //stack segment pointer

    reg_t ss;
    //flags
    reg_t eflags;
    //stack head pointer
    reg_t esp;
    //base pointer
    reg_t ebp;
};


struct th
{
    prior_t prior;
    bil_t bills;
    ith_t id;
    iproc_t own_proc_id;
    mem_map regs;
    //instruction pointer
    reg_t eip;
    state_t state;



    //don't care, need to verify
    int save_state()
    {
        reg_t t;
        asm("movl %eax,t");
        this->regs.eax=t;
        asm("movl %ebx,t");
        this->regs.ebx=t;
        asm("movl %ecx,t");
        this->regs.ecx=t;
        asm("movl %edx,t");
        this->regs.edx=t;

        asm("movl %ss,t");
        this->regs.ss=t;
        asm("movl %eflags,t");
        this->regs.eflags=t;
        asm("movl %esp,t");
        this->regs.esp=t;
        asm("movl %ebp,t");
        this->regs.ebp=t;

        asm("movl %eip,t");
        this->eip=t;
        return SUCCESS;
    }

    int restore_state()
    {
        reg_t t;
        t=this->regs.eax;
        asm("movl t,%eax");
        t=this->regs.ebx;
        asm("movl t,%ebx");
        t=this->regs.ecx;
        asm("movl t,%ecx");
        t=this->regs.edx;
        asm("movl t,%edx");

        t=this->regs.ss;
        asm("movl t,%ss");
        t=this->regs.eflags;
        asm("movl t,%eflags");
        t=this->regs.esp;
        asm("movl t,%esp");
        t=this->regs.ebp;
        asm("movl t,%ebp");

        return SUCCESS;
    }
};

static struct th th_table[MAX_K_TH*MAX_PROC];
static short int th_count=0;

int find_th(ith_t id)
{
    int i;
    for(i=0;i<th_count;i++)
    {
        if(id==th_table[i].id)
        return i;
    }
    return FAIL;
}
